package com.zcf.controller;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zcf.common.json.Body;
import com.zcf.pojo.Dining;
import com.zcf.pojo.Notice;
import com.zcf.pojo.User;
import com.zcf.service.NoticeService;
import com.zcf.utils.UploadImgUtils;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Controller
@RequestMapping("/notice")
@CrossOrigin
@ResponseBody
public class NoticeController {
	@Autowired
	private NoticeService noticeService;

	/**
	 * 新增公告
	 */
	@PostMapping("/setNotice")
	public Body setNotice(String userId, String content, String title, MultipartFile[] file, String din_id) {
		try {
			String files = UploadImgUtils.uploadFiles(file);
//			if (files != null && !"".equals(files)) {
			return noticeService.setNotice(userId, content, title, files, din_id);
//			}
//			return Body.newInstance(501, "照片不能为空");
		} catch (Exception e) {
			e.printStackTrace();
 			return Body.newInstance(501, "上传报错");
		}
	}

	/**
	 * 查询店铺公告
	 */
	@PostMapping("getNotice")
	public Body getNotice(String din_id) {
		Notice notice = new Notice();
		EntityWrapper<Notice> ew = new EntityWrapper<>();
		if (din_id != null) {
			ew.eq("din_id", din_id);
		}
		ew.orderBy("time", false);
		List<Notice> lists = notice.selectList(ew);
		List<Notice> list = new ArrayList<>();
		List<Notice> lista = new ArrayList<>();
		for (Notice notice2 : lists) {
			if (din_id == null) {
				if (notice2.getDin_id() == null) {
					list.add(notice2);
				}
			}
			if (din_id != null) {
				lista.add(notice2);
			}
		}
		if (lists.size() < 0) {
			return Body.newInstance(-1, "查无数据");
		}
		if (din_id == null) {
			return Body.newInstance(list);
		} else {
			return Body.newInstance(lista);
		}
	}

	/**
	 * 修改公告
	 */
	@PostMapping("updateNotice")
	public Body updateNotice(String id, String userId, String content, String title, MultipartFile[] file,
			String din_id) {
		try {
			String files = UploadImgUtils.uploadFiles(file);
			if (files != null && !"".equals(files)) {
				return noticeService.updateNotice(id, userId, content, title, files, din_id);
			}else{
				return Body.newInstance(501, "未修改图片");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return Body.newInstance(501, "上传报错");
		}
	}

	/**
	 * 删除公告
	 */
	@PostMapping("delecteNotice")
	public Body delecteNotice(String id) {
		Notice notice = new Notice();
		EntityWrapper<Notice> ew = new EntityWrapper<>();
		ew.eq("id", id);
		Boolean flag = notice.delete(ew);
		if (flag) {
			return Body.newInstance("删除成功");
		}
		return Body.newInstance(-1, "删除失败");
	}

	/**
	 * 系统公告
	 */
	@PostMapping("querySystem")
	public Body querySystem(String title, String time) {
		EntityWrapper<Notice> ew = new EntityWrapper<>();
		if (title != null) {
			ew.like("title", title);
		}
		if (time != null) {
			ew.like("time", time);
		}
		Notice notice = new Notice();
		List<Notice> lists = notice.selectList(ew);
		List<Notice> list = new ArrayList<>();
		for (Notice notice2 : lists) {
			if (notice2.getDin_id() != null && notice2.getDin_id() != "") {
				continue;
			}
			list.add(notice2);
		}
		if (list.size() > 0) {
			return Body.newInstance(list);
		}
		return Body.BODY_500;
	}

	/**
	 * 店铺公告
	 */
	@PostMapping("queryDining")
	public Body queryDining(String dining_name, String time) {
		EntityWrapper<Notice> ew = new EntityWrapper<>();
		if (time != null) {
			ew.like("time", time);
		}
		if (dining_name != null) {
			ew.like("din_name", dining_name);
		}
		Notice notice = new Notice();
		List<Notice> lists = notice.selectList(ew);
		if (lists.size() < 0) {
			return Body.BODY_500;
		}
		List<Notice> list = new ArrayList<>();
		for (Notice notice2 : lists) {
			Dining dining = new Dining();
			EntityWrapper<Dining> ews = new EntityWrapper<>();
			if (notice2.getDin_id() != null && notice2.getDin_id() != "") {
				ews.eq("id", notice2.getDin_id());
				dining = dining.selectOne(ew);
				User user = new User();
				user.setId(notice2.getId());
				user = user.selectById();
				if (user != null) {
					notice2.setUserName(user.getName());
				}
				if (dining != null) {
					notice2.setDinName(dining.getDiningName());
				}
				list.add(notice2);
			}
			continue;
		}
		if (list.size() > 0) {
			return Body.newInstance(list);
		}
		return Body.BODY_500;
	}

	/**
	 * 查询单个公告
	 */
	@PostMapping("queryOne")
	public Body queryOne(String id) {
		Notice notice = new Notice();
		notice.setId(id);
		notice = notice.selectById();
		if (notice != null) {
			return Body.newInstance(notice);
		}
		return Body.newInstance();
	}


}
